package com.wangzeqi.neihanduanzi.news.adapter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.news.base.Base;
import com.wangzeqi.neihanduanzi.news.db.DaoMaster;
import com.wangzeqi.neihanduanzi.news.db.DaoSession;
import com.wangzeqi.neihanduanzi.news.db.HeadLine;
import com.wangzeqi.neihanduanzi.news.db.HeadLineDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/26.
 */
public class NewsAdapter extends BaseAdapter{
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private List<HeadLine> list;


    private RequestQueue queue;

    private List<Base.T1348647909107> base1;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;

    private Context context;

    public void setBase1(List<Base.T1348647909107> base1) {
        this.base1 = base1;
        notifyDataSetChanged();
        initDatabase();
    }


    public NewsAdapter(Context context) {
        list = new ArrayList<>();
        queue = Volley.newRequestQueue(context);
        this.context = context;
        list =initDatabases();
    }
    public void getBase(List<Base.T1348647909107> base1){
        this.base1 =base1;
        notifyDataSetChanged();
    }

    public List<HeadLine> initDatabases() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "headline.db", null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        HeadLineDao headLineDao = daoSession.getHeadLineDao();

        List<HeadLine> queryList = headLineDao.queryBuilder().listLazy();
        db.close();
        return queryList;

    }

    private void initDatabase() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "headline.db", null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        HeadLineDao headLineDao = daoSession.getHeadLineDao();
        headLineDao.deleteAll();

        for (int i = 0; i < base1.size(); i++) {

            String url1 = null;
            String url2 = null;
            int j;
            if (base1.get(i).getImgnewextra() != null) {
                url1 = base1.get(i).getImgnewextra().get(0).getImgsrc();
                url2 = base1.get(i).getImgnewextra().get(1).getImgsrc();
                j = base1.get(i).getImgnewextra().size();
            } else {
                j = 0;
            }
            HeadLine headLine = new HeadLine((long) i, base1.get(i).getTitle(), base1.get(i).getDisgest(),
                    base1.get(i).getReplyCount(), base1.get(i).getImgsrc(), base1.get(i).getImgType(), j, url1, url2);
            headLineDao.insert(headLine);
        }
    }

    @Override
    public int getCount() {

        if (base1 != null && base1.size() > 0) {
            return base1.size();
        } else if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int i) {
        if (base1 != null) {
            if (i==0){
                return -1;
            }
            else if (base1.get(i).getImgnewextra() != null) {

                return TYPE_3;

            } else if (base1.get(i).getImgType() == 1)
                return TYPE_1;
            else
                return TYPE_2;
        } else {
            if (i==0){
                return -1;
            }
            else  if (list.get(i).getImageType() == 0 && list.get(i).getImagenum() != 0) {
                return TYPE_3;

            } else if (list.get(i).getImageType() == 1)
                return TYPE_1;
            else
                return TYPE_2;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    public void getImage(NetworkImageView imageView, String url) {
        final ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }
        });
        imageView.setImageUrl(url, imageLoader);

    }

    public void getImage(String url, String url1, String url2, NetworkImageView imageView, NetworkImageView imageView1
            , NetworkImageView imageView2) {
        final ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {

            }
        });
        imageView.setImageUrl(url, imageLoader);
        final ImageLoader imageLoader1 = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url1) {
                return null;
            }

            @Override
            public void putBitmap(String url1, Bitmap bitmap) {

            }
        });
        imageView1.setImageUrl(url1, imageLoader1);
        final ImageLoader imageLoader2 = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url2) {
                return null;
            }
            @Override
            public void putBitmap(String url2, Bitmap bitmap) {
            }
        });
        imageView2.setImageUrl(url2, imageLoader2);
    }
    @Override
    public Object getItem(int i) {

        if (base1 != null && base1.size() > 0) {
            return base1.get(i);
        } else if (list != null && list.size() > 0) {
            return list.get(i);
        }
        return null;
    }
    @Override
    public long getItemId(int i) {
        if (base1 != null && base1.size() > 0) {
            return i;
        } else if (list != null && list.size() > 0) {
            return i;
        }
        return 0;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        String url = null;
        if (base1 != null) {
            url = base1.get(i).getImgsrc();
        } else {
            url = list.get(i).getImgsrc();
        }
        Holder1 holder1 = null;
        Holder2 holder2 = null;
        Holder3 holder3 = null;
        Holder4 holder4 = null;
        int type = getItemViewType(i);
        if (view == null) {
            switch (type) {
                case -1:
                    view = LayoutInflater.from(context).inflate(R.layout.headlineitemnull,null);
                    holder4 = new Holder4();
                    view.setTag(holder4);
                    break;
                case TYPE_2:
                    view = LayoutInflater.from(context).inflate(R.layout.headlineitemleft, null);
                    holder2 = new Holder2();
                    holder2.headlinetitleleft = (TextView) view.findViewById(R.id.tv_left_title);
                    holder2.haedlinebodyleft = (TextView) view.findViewById(R.id.tv_left_body);
                    holder2.headlineleftiv = (NetworkImageView) view.findViewById(R.id.iv_left);
                    view.setTag(holder2);
                    break;
                case TYPE_1:
                    view = LayoutInflater.from(context).inflate(R.layout.headlineitemfull, null);
                    holder1 = new Holder1();
                    holder1.headlinrtitle = (TextView) view.findViewById(R.id.tv_full_title);
                    holder1.headlinebody = (TextView) view.findViewById(R.id.tv_full_body);
                    holder1.headlinesay = (TextView) view.findViewById(R.id.tv_full_say);
                    holder1.headlinefulliv = (NetworkImageView) view.findViewById(R.id.iv_full);
                    view.setTag(holder1);
                    break;
                case TYPE_3:
                    view = LayoutInflater.from(context).inflate(R.layout.headlineitemthree, null);
                    holder3 = new Holder3();
                    holder3.headlinetitlethree = (TextView) view.findViewById(R.id.tv_three_title);
                    holder3.headlineleft = (NetworkImageView) view.findViewById(R.id.iv_three_left);
                    holder3.headlinecenter = (NetworkImageView) view.findViewById(R.id.iv_center);
                    holder3.headlinerignt = (NetworkImageView) view.findViewById(R.id.iv_right);
                    view.setTag(holder3);
                    break;
            }
        } else {
            if (i == 1){
                switch (type){
                    case TYPE_1:
                        Holder1 holder11 = new Holder1();
                        holder11.headlinrtitle = (TextView) view.findViewById(R.id.tv_full_title);
                        holder11.headlinebody = (TextView) view.findViewById(R.id.tv_full_body);
                        holder11.headlinesay = (TextView) view.findViewById(R.id.tv_full_say);
                        view.setTag(holder11);
                        break;
                    case TYPE_2:
                        Holder2 holder22 = new Holder2();
                        holder22.headlinetitleleft = (TextView) view.findViewById(R.id.tv_left_title);
                        holder22.haedlinebodyleft = (TextView) view.findViewById(R.id.tv_left_body);
                        holder22.headlineleftiv = (NetworkImageView) view.findViewById(R.id.iv_left);
                        view.setTag(holder22);
                        break;
                    case TYPE_3:
                        Holder3 holder33 = new Holder3();
                        holder33.headlinetitlethree = (TextView) view.findViewById(R.id.tv_three_title);
                        holder33.headlineleft = (NetworkImageView) view.findViewById(R.id.iv_three_left);
                        holder33.headlinecenter = (NetworkImageView) view.findViewById(R.id.iv_center);
                        holder33.headlinerignt = (NetworkImageView) view.findViewById(R.id.iv_right);
                        view.setTag(holder33);
                        break;
                }
            }
            switch (type) {
                case -1:
                    holder4 = (Holder4) view.getTag();
                    break;
                case TYPE_2:
                    holder2 = (Holder2) view.getTag();
                    break;
                case TYPE_1:
                    holder1 = (Holder1) view.getTag();
                    break;
                case TYPE_3:
                    holder3 = (Holder3) view.getTag();
                    break;
            }
        }
        if (base1 != null) {
            switch (type) {

                case TYPE_2:
                    holder2.headlinetitleleft.setText(base1.get(i).getTitle());
                    holder2.haedlinebodyleft.setText(base1.get(i).getDisgest());
                    holder2.headlineleftiv.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder2.headlineleftiv.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    getImage(holder2.headlineleftiv, url);
                    break;
                case TYPE_1:
                    holder1.headlinrtitle.setText(base1.get(i).getTitle());
                    holder1.headlinebody.setText(base1.get(i).getDisgest());
                    holder1.headlinesay.setText(String.valueOf(base1.get(i).getReplyCount()));
                    holder1.headlinefulliv.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder1.headlinefulliv.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    getImage(holder1.headlinefulliv, url);
                    break;
                case TYPE_3:
                    String url1 = base1.get(i).getImgnewextra().get(0).getImgsrc();
                    String url2 = base1.get(i).getImgnewextra().get(1).getImgsrc();
                    // Log.d("HeadLineListViewAdapter", base1.get(i).getTitle());
                    holder3.headlinetitlethree.setText(base1.get(i).getTitle());
                    holder3.headlineleft.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder3.headlinecenter.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder3.headlinerignt.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder3.headlineleft.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    holder3.headlineleft.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    holder3.headlineleft.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    getImage(url, url1, url2, holder3.headlineleft, holder3.headlinecenter, holder3.headlinerignt);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_2:
                    holder2.headlinetitleleft.setText(list.get(i).getTitle());
                    holder2.haedlinebodyleft.setText(list.get(i).getDisgest());
                    holder2.headlineleftiv.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder2.headlineleftiv.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    getImage(holder2.headlineleftiv, url);
                    break;
                case TYPE_1:
                    holder1.headlinrtitle.setText(list.get(i).getTitle());
                    holder1.headlinebody.setText(list.get(i).getDisgest());
                    holder1.headlinefulliv.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder1.headlinefulliv.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    getImage(holder1.headlinefulliv, url);
                    break;
                case TYPE_3:
                    String url1 = list.get(i).getImgsrc1();
                    String url2 = list.get(i).getImgsrc2();
                    holder3.headlinetitlethree.setText(initDatabases().get(i).getTitle());
                    holder3.headlineleft.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder3.headlinecenter.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder3.headlinerignt.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
                    holder3.headlineleft.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    holder3.headlineleft.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    holder3.headlineleft.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
                    getImage(url, url1, url2, holder3.headlineleft, holder3.headlinecenter, holder3.headlinerignt);
                    break;
            }
        }
        return view;
    }
    class Holder1 {
        TextView headlinrtitle;
        TextView headlinebody;
        NetworkImageView headlinefulliv;
        TextView headlinesay;
    }

    class Holder2 {
        TextView headlinetitleleft;
        TextView haedlinebodyleft;
        NetworkImageView headlineleftiv;

    }

    class Holder3 {
        TextView headlinetitlethree;
        NetworkImageView headlineleft;
        NetworkImageView headlinecenter;
        NetworkImageView headlinerignt;

    }
    class Holder4{

    }
}
